define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, $timeout, Productmanager, Auth, GoCatTypeAuth, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, $ionicPopup, GoDialog, $ionicModal, $window) {

		$scope.$on('$ionicView.afterEnter', function () {
			$scope.init_data();
			$scope.init_data_auth();
			//得到当前用户身份。1:开发商，2:品牌代理商，3:代理商/分仓	4:经销商
			$scope.user_type = Auth.check_user_type();
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins&&ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = $window.innerHeight;
			$scope.myObj = {
				"height": all - 44*3 - lan - 34 + "px"
			};
			*/
			//动态设置高度
			GoPage.init_top($scope, 44, 44, 88);
		}, false);


		$scope.sort = [{id:1,name:"默认排序"},{id:2,name:"价格从高到低"},{id:3,name:"价格从低到高"},{id:4,name:"销量从高到低"},{id:5,name:"销量从低到高"},{id:6,name:"发布时间从远到近"},{id:7,name:"发布时间从近到远"}];
		$scope.input = new Object();
		$scope.input.order_select = {id:1,name:"默认排序"};
		$scope.input.order = "1"; //默认1
		$scope.order_sale = "1";//销售中
		$scope.order_wait = "1";//待上架
		$scope.filterString = "";
		//滑动切换--一级
		$scope.selectTab = 0;
		$scope.activeSlide = function (index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			if ($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
			$scope.checkIsAll();//判断是否全选
		};
		$scope.slideChanged = function (index) {
			$scope.selectTab = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function () {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function () {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		//打开侧边的筛选菜单
		$scope.openRightMenu = function () {
			$ionicSideMenuDelegate.toggleRight();
		};

		//4个编辑的小图标
		$scope.onshow_productmanager_publish_edit = function () {
			GoPage.go_states("productmanager_sale_edit");
		};

		$scope.onshow_productmanager_waitsale_edit = function () {
			GoPage.go_states("productmanager_waitsale_edit");
		};
		$scope.doSelectSort = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择排序",$scope.sort,$scope.input.order_select,function(data){
				$scope.input.order = data.id;
				$scope.input.order_select = data;
				$scope.orderby();
			});
		}
		//得到销售中的商品(后面设置新品、热卖、免邮要用到，估将它从init_data中提取出来)
		$scope.getSalingGoods = function () {
			$scope.nPage_sale = 1; //销售中
			Productmanager.goods_search($scope.input.key, 1, $scope.order_sale, $scope.filterString, true, function (data) {
				$scope.data_goods = data;
				//				console.log(data.goods)
			});
		};
		//得到待上架的商品
		$scope.getWaitSaleGoods = function () {
			$scope.nPage_waitsale = 1; //待上架
			Productmanager.goods_search($scope.input.key, 1, $scope.order_wait, $scope.filterString, false, function (data) {
				$scope.goods_waitSale = data;
			});
		};

		//初始化页面
		$scope.init_data = function () {
			//品牌
			Productmanager.brands(function (data) {
				if (data != null) {
					$scope.brands = data.brand;
				}
			})
			//销售中商品
			$scope.getSalingGoods();
			//待上架商品
			$scope.getWaitSaleGoods();
			//商品标签一览
			Productmanager.getTagList(function (data) {
				$scope.tagList = data.result;
			})
		};
		$scope.doSelectBrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择品牌",$scope.brands,$scope.filter.brand_select,function(data){
				$scope.filter.brand_id[0] = data.id;
				$scope.filter.brand_select = data;
			});
		}
		//(1)上下架--单个
		$scope.goods_update = function (goods_id, marketable) {
			Productmanager.goods_update(goods_id, marketable, function (data) {
				if (data.success) {
					GoDialog.toast("操作成功！");
					if ($scope.selectTab == 0) {
						$scope.delete_item($scope.data_goods.goods, goods_id); //删除一个元素
					} else {
						$scope.delete_item($scope.goods_waitSale.goods, goods_id); //删除一个元素
					}
				} else {
					GoDialog.toast(data.msg);
				}
			})
		};

		//获取商品id
		$scope.getGoodsId = function (dataArr) {
			if (!dataArr) return;
			var goods_ids = "";
			for (var i = 0; i < dataArr.length; i++) {
				if (dataArr[i].isCheckIcon) {
					goods_ids += dataArr[i].goods_id + ",";
				}
			}
			if (goods_ids.endsWith(",")) {
				goods_ids = goods_ids.substring(0, goods_ids.length - 1);
			}
			return goods_ids;
		}

		//上下架--多个
		$scope.goods_update_many = function () {
			var goods_ids = "";
			var marketable = "";
			if ($scope.selectTab == 0) {//销售中--下架
				goods_ids = $scope.getGoodsId($scope.data_goods.goods);
				marketable = false;
			}
			if ($scope.selectTab == 1) {//待上架--上架
				goods_ids = $scope.getGoodsId($scope.goods_waitSale.goods);
				marketable = true;
			}
			if (!goods_ids) {
				GoDialog.toast("请先选择商品！");
				return;
			}
			Productmanager.goods_update(goods_ids, marketable, function (data) {
				GoDialog.toast("操作成功！");
				$scope.select_goods();//重新初始化页面
			})
		}


		//全选
		$scope.getAll = function () {
			if ($scope.selectTab == 0) {//销售中
				if (!$scope.data_goods.goods) return;
				for (var i = 0; i < $scope.data_goods.goods.length; i++) {
					if ($scope.data_goods.goods[i].isCheckIcon) {
						$scope.data_goods.goods[i].isCheckIcon = false;
					} else {
						$scope.data_goods.goods[i].isCheckIcon = true;
					}
				}
			}
			if ($scope.selectTab == 1) {//待上架
				if (!$scope.goods_waitSale.goods) return;
				for (var i = 0; i < $scope.goods_waitSale.goods.length; i++) {
					if ($scope.goods_waitSale.goods[i].isCheckIcon) {
						$scope.goods_waitSale.goods[i].isCheckIcon = false;
					} else {
						$scope.goods_waitSale.goods[i].isCheckIcon = true;
					}
				}
			}
			$scope.checkIsAll();//判断是否全选，必须放在后面
		}
		//单选
		$scope.getOne = function (index, $event) {
			$event.stopPropagation();//阻止事件冒泡
			if ($scope.selectTab == 0) {//销售中
				if ($scope.data_goods.goods[index].isCheckIcon) {
					$scope.data_goods.goods[index].isCheckIcon = false;
				} else {
					$scope.data_goods.goods[index].isCheckIcon = true;
				}
			}
			if ($scope.selectTab == 1) {//待上架
				if ($scope.goods_waitSale.goods[index].isCheckIcon) {
					$scope.goods_waitSale.goods[index].isCheckIcon = false;
				} else {
					$scope.goods_waitSale.goods[index].isCheckIcon = true;
				}
			}
			$scope.checkIsAll();//判断是否全选
		}
		//判断是否全选，控制底部的全选按钮，该方法必须放在其他方法的后面
		$scope.checkIsAll = function () {
			if ($scope.selectTab == 0) {//销售中
				if (!$scope.data_goods.goods) return;
				for (var i = 0; i < $scope.data_goods.goods.length; i++) {
					if ($scope.data_goods.goods[i].isCheckIcon) {
						$scope.isCheckIconAll = true;
					} else {
						$scope.isCheckIconAll = false;
						break;
					}
				}
			}
			if ($scope.selectTab == 1) {//待上架
				if (!$scope.goods_waitSale.goods) return;
				for (var i = 0; i < $scope.goods_waitSale.goods.length; i++) {
					if ($scope.goods_waitSale.goods[i].isCheckIcon) {
						$scope.isCheckIconAll = true;
					} else {
						$scope.isCheckIconAll = false;
						break;
					}
				}
			}
		}


		//删除商品，"销售中商品"和"待上架商品"共用该方法
		//		$scope.goods_delete = function(goods_id) {
		//			Productmanager.goods_delete(goods_id, function(data) {
		//				if(data.success) {
		//					GoDialog.toast("删除成功！");
		//					if($scope.selectTab == 0) {
		//						$scope.delete_item($scope.data_goods.goods, goods_id); //删除一个元素
		//					} else {
		//						$scope.delete_item($scope.goods_waitSale.goods, goods_id); //删除一个元素
		//					}
		//				} else {
		//					GoDialog.toast(data.msg);
		//				}
		//			})
		//		};
		//删除现有数据数组中的一个元素--公用--“上下架”、“删除商品”共用
		$scope.delete_item = function (datas, goods_id) {
			var index = -1;
			for (var i = 0; i < datas.length; i++) {
				if (datas[i].goods_id == goods_id) {
					index = i;
					break; //一找到就退出循环
				}
			}
			if (index != -1) { //说明找到对应的商品
				datas.splice(index, 1);
			}
		};

		//共用判断--排序公用
		$scope.select_goods = function () {
			if ($scope.selectTab == 0) { //如果是“销售中商品”，则执行该方法
				$scope.getSalingGoods();
			}
			if ($scope.selectTab == 1) { //如果是“待上架商品”，则执行该方法
				$scope.getWaitSaleGoods();
			}
			$scope.isCheckIconAll = false;
		};

		//排序
		$scope.orderby = function () {
			if ($scope.selectTab == 0) {
				$scope.order_sale = $scope.input.order;
			}
			if ($scope.selectTab == 1) {
				$scope.order_wait = $scope.input.order;
			}
			$scope.select_goods();
		};

		var checkboxs = document.getElementsByClassName("catCheckbox");
		$scope.checkItem = function () {
			var selectAll = document.getElementById("selectAll");
			var selectedLen = 0;
			for (var i = 0; i < checkboxs.length; i++) {
				if (checkboxs[i].checked) {
					selectedLen++;
				}
			}
			if (selectedLen == checkboxs.length) {
				selectAll.checked = true;
			} else {
				selectAll.checked = "";
			}
		};

		/***分类三级联动***/
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听
		$scope.listen_cat1 = function (data) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			$scope.cat3_items = '';
		};
		$scope.listen_cat2 = function (data) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = {
			name: 'input.cat1'
		};
		var obj2_cat = {
			name: 'input.cat2'
		};
		var obj3_cat = {
			name: 'input.cat3'
		};
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
		//分类三级联动
		//GoCatTypeAuth.init_load_only($scope, items_cat);//改到主接口调用完毕后，再调用
		$scope.doSelectCat1 = function () {
			//id是值 name是显示
			GoDialog.showSelect("大分类", $scope.cat1_items, $scope.input.cat1, function (data) {
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function () {
			//id是值 name是显示
			GoDialog.showSelect("中分类", $scope.cat2_items, $scope.input.cat2, function (data) {
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function () {
			//id是值 name是显示
			GoDialog.showSelect("小分类", $scope.cat3_items, $scope.input.cat3, function (data) {
				$scope.input.cat3 = data;
			});
		}
		$scope.init_filter = function () {
			$scope.input.key = "";
			$scope.filter = new Object();
			$scope.filter.bn = ""; //商品编码
			$scope.filter.brand_id = new Array(); //品牌数组
			$scope.filter.brand_select = {id:0,name:"请选择品牌"};
			$scope.filter.cat_id = new Array(); //品类数组
			$scope.filter.price_from = "";
			$scope.filter.price_to = "";
			$scope.filter.custom_cat_id = ""; //店铺自定义分类ID
			$scope.filter.goods_state = "";
		};
		$scope.init_filter(); //初始化搜索条件

		$scope.getType = function ($event, value) {
			var target = $event.target;
			var xzs = document.getElementsByClassName("xz");
			for (var i = 0; i < xzs.length; i++) {
				xzs[i].className = "xz notShow";
				xzs[i].parentNode.className = "col";
			}
			if ($scope.filter.goods_state == value) {
				$scope.filter.goods_state = "";
			} else {
				$scope.filter.goods_state = value;
				for (var i = 0; i < target.children.length; i++) {
					target.className = "col activeType";
					target.children[i].className = "xz";
				}
			}
		};

		//重置
		$scope.reset = function () {
			$scope.init_filter(); //初始化搜索条件
			$scope.input.cat1 = null;
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
		};
		//侧边的筛选
		$scope.search = function () {
			if ($scope.input.cat1 == null) $scope.filter.cat_id[0] = "";
			if ($scope.input.cat2 == null) $scope.filter.cat_id[1] = "";
			if ($scope.input.cat3 == null) $scope.filter.cat_id[2] = "";
			if ($scope.input.cat1) $scope.filter.cat_id[0] = $scope.input.cat1.id;
			if ($scope.input.cat2) $scope.filter.cat_id[1] = $scope.input.cat2.id;
			if ($scope.input.cat3) $scope.filter.cat_id[2] = $scope.input.cat3.id;

			$scope.filterString = JSON.stringify($scope.filter);
			$scope.select_goods(); //不同的商品类型查询
			if ($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
		};

		/*===============上下拉刷新==================*/
		$scope.nPage_sale = 1; //销售中
		$scope.nPage_waitsale = 1; //待上架
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function () {
			$scope.isLoad = true;
			if ($scope.selectTab == 0) { //销售中商品
				Productmanager.goods_search($scope.input.key, 1, $scope.order_sale, $scope.filterString, true, function (data) {
					if (data != null) {
						$scope.data_goods = data;
					} else {
						$scope.data_goods = new Object();
					}
					$scope.nPage_sale = 1;
				}, function () {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function () {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			if ($scope.selectTab == 1) { //待上架商品
				Productmanager.goods_search($scope.input.key, 1, $scope.order_wait, $scope.filterString, false, function (data) {
					if (data != null) {
						$scope.goods_waitSale = data;
					} else {
						$scope.goods_waitSale = new Object();
					}
					$scope.nPage_waitsale = 1;
				}, function () {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function () {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		};

		//加载更多
		$scope.doRefreshMore = function () {
			$scope.isLoad = true;
			if ($scope.selectTab == 0) { //销售中商品
				Productmanager.goods_search($scope.input.key, $scope.nPage_sale + 1, $scope.order_sale, $scope.filterString, true, function (data) {
					if (data.goods != null) {
						if ($scope.data_goods.goods != undefined) {
							$scope.data_goods.goods = $scope.data_goods.goods.concat(data.goods);
							$scope.nPage_sale += 1;
						} else {
							$scope.data_goods = data;
						}
					}
				}, function () {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function () {
						$scope.isLoad = false;
					}, 1500);
				});
			}
			if ($scope.selectTab == 1) { //待上架
				Productmanager.goods_search($scope.input.key, $scope.nPage_waitsale + 1, $scope.order_wait, $scope.filterString, false, function (data) {
					if (data.goods != null) {
						if ($scope.goods_waitSale.goods != undefined) {
							$scope.goods_waitSale.goods = $scope.goods_waitSale.goods.concat(data.goods);
							$scope.nPage_waitsale += 1;
						} else {
							$scope.goods_waitSale = data;
						}
					}
				}, function () {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function () {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		};
		/*===============上下拉刷新==================*/

		//删除input.key
		$scope.initKey = function () {
			$scope.input.key = "";
		};

		//设置新品
		$scope.addGoodsTag = function () {
			var goods_ids = "";
			var tag_id_list = "";
			if ($scope.goods_ids_list) {//单个
				goods_ids = $scope.goods_ids_list;
			} else {
				if ($scope.selectTab == 0) {//销售中--下架
					goods_ids = $scope.getGoodsId($scope.data_goods.goods);
				}
				if ($scope.selectTab == 1) {//待上架--上架
					goods_ids = $scope.getGoodsId($scope.goods_waitSale.goods);
				}
			}
			tag_id_list = $scope.getTagId();

			if (!goods_ids) {
				GoDialog.toast("请先选择商品！");
				return;
			}
			//			if(!tag_id_list){//当不选择标签时，表示去掉该标签
			//				GoDialog.toast("请先选择要添加的商品标签！");
			//				return;
			//			}
			Productmanager.addGoodsTag(goods_ids, tag_id_list, function (data) {
				GoDialog.toast(data.msg);
				$scope.closeModal_tag();
				$scope.init_data();
			});
		}

		//选择商品标签
		$scope.selectTagId = function (index) {
			if ($scope.tagList[index].isSelected) {
				$scope.tagList[index].isSelected = false;
			} else {
				$scope.tagList[index].isSelected = true;
			}
		}
		//获取商品标签id
		$scope.getTagId = function () {
			var tag_id_list = "";
			for (var i = 0; i < $scope.tagList.length; i++) {
				if ($scope.tagList[i].isSelected) {
					tag_id_list += $scope.tagList[i].tag_id + ",";
				}
			}
			if (tag_id_list.endsWith(",")) {
				tag_id_list = tag_id_list.substring(0, tag_id_list.length - 1);
			}
			return tag_id_list;
		}



		//给商品添加标签
		$ionicModal.fromTemplateUrl("templates/modal/addGoodsTag.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function (modal) {
			$scope.modal = modal;
		});
		//打开
		$scope.openModal_tag = function (goods_id) {
			$scope.goods_ids_list = goods_id;
			$scope.modal.show();
		}
		//隐藏
		$scope.closeModal_tag = function () {
			$scope.modal.hide();
		}

		//		var htmlEl = angular.element(document.querySelector('html'));
		//		htmlEl.on('click', function(event) {
		//			if(event.target.nodeName === 'HTML') {
		//				if(myPopup) {
		//					myPopup.close();
		//				}
		//			}
		//		});

		//页面跳转
		$scope.onshow_detail = function (goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, 'type': "goods" });
		}

		//权限控制，切换到指定的tab页面
		$scope.init_data_auth = function () {
			Auth.get_all_user_auth(function (isHave, items) {
				//授权
				if (items['销售中商品']) {
					$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(0);
				} else if (items['待上架商品']) {
					$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(1);
				}
			});
		}

	}

	controller.$inject = ['$scope', '$timeout', 'Productmanager', 'Auth', 'GoCatTypeAuth', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate', 'GoPage', '$ionicPopup', 'GoDialog', '$ionicModal', '$window'];

	return app.registerController("productmanagerctrl", controller);
});